package com.xyz.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xyz.dto.EmContainerDto;
import com.xyz.dto.EmPipingDto;
import com.xyz.dto.SpecialPageAllDto;
import com.xyz.entity.mapper.EquipmentInfoMapper;
import com.xyz.entity.mapper.SpecialEmContainerMapper;
import com.xyz.entity.mapper.SpecialEmPipingMapper;
import com.xyz.entity.pojo.*;
import com.xyz.service.CommonService;
import com.xyz.service.SpecialEmService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.GridData;
import com.xyz.util.dto.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class SpecialEmServiceImpl implements SpecialEmService {


    @Autowired
    private SpecialEmPipingMapper specialEmPipingMapper;

    @Autowired
    private SpecialEmContainerMapper specialEmContainerMapper;

    @Autowired
    private CommonService commonService;

    @Autowired
    private EquipmentInfoMapper equipmentInfoMapper;

    @Override
    public DataResult getEmPipingList(LoginInfo loginInfo, SpecialPageAllDto specialPageAllDto) throws Exception {
        SpecialEmPipingExample example = new SpecialEmPipingExample();
        SpecialEmPipingExample.Criteria eiCriteria = example.createCriteria();
        eiCriteria.andIsDeleteEqualTo(0);
        if (StrUtil.isNotBlank(specialPageAllDto.getName())) {
            eiCriteria.andNameLike("%" + specialPageAllDto.getName() + "%");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StrUtil.isNotEmpty(specialPageAllDto.getStartTime())) {
            eiCriteria.andCreateTimeGreaterThanOrEqualTo(sdf.parse(specialPageAllDto.getStartTime() + " 00:00:00"));
        }
        if (StrUtil.isNotEmpty(specialPageAllDto.getEndTime())) {
            eiCriteria.andCreateTimeLessThanOrEqualTo(sdf.parse(specialPageAllDto.getEndTime() + " 23:59:59"));
        }
        example.setOrderByClause("create_time desc");
        Page<SpecialEmPiping> page = PageHelper.startPage(NumberUtil.parseInt(specialPageAllDto.getPageNum()), NumberUtil.parseInt(specialPageAllDto.getPageSize()));

        List<SpecialEmPiping> emPipingList = specialEmPipingMapper.selectByExample(example);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<Integer, String> containerDepartment = commonService.getDictMap("CONTAINER_DEPARTMENT");
        for (SpecialEmPiping sep : emPipingList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", sep.getId());
            map.put("deviceName", sep.getDeviceName());
            map.put("pipeCode", sep.getPipeCode());
            map.put("name", sep.getName());
            map.put("emLocation", sep.getEmLocation());
            map.put("pipeLength", sep.getPipeLength());
            map.put("specification", sep.getSpecification());
            map.put("emRegion", sep.getEmRegion());
            map.put("registerCode", sep.getRegisterCode());
            map.put("checkAccount", sep.getCheckAccount());
            map.put("instalUnit", sep.getInstalUnit());
            if(sep.getNextCheckDate() != null){
                map.put("nextCheckDate", DateUtil.format(sep.getNextCheckDate(), "yyyy-MM-dd"));
            }else {
                map.put("nextCheckDate", sep.getNextCheckDate());
            }
            if(sep.getNextCheckDate() != null){
                map.put("usedDate", DateUtil.format(sep.getUsedDate(), "yyyy-MM-dd"));
            }else {
                map.put("usedDate", sep.getUsedDate());
            }
            map.put("departmentName", containerDepartment.get(sep.getDepartment()));
            map.put("createTime", DateUtil.format(sep.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            list.add(map);
        }
        return DataResult.build9100(new GridData(page.getTotal(), list));
    }

    @Override
    public DataResult insertEmPiping(LoginInfo loginInfo, EmPipingDto emPipingDto) throws Exception {
        SpecialEmPiping info = new SpecialEmPiping();
        info.setName(emPipingDto.getName());
        info.setDeviceName(emPipingDto.getDeviceName());
        info.setDepartment(emPipingDto.getDepartment() == null ? null : Integer.parseInt(emPipingDto.getDepartment()));
        info.setPipeCode(emPipingDto.getPipeCode());
        info.setCheckAccount(emPipingDto.getCheckAccount());
        info.setEmLocation(emPipingDto.getEmLocation());
        info.setEmRegion(emPipingDto.getEmRegion());
        info.setPipeLength(emPipingDto.getPipeLength());
        info.setRegisterCode(emPipingDto.getRegisterCode());
        info.setSpecification(emPipingDto.getSpecification());
        info.setInstalUnit(emPipingDto.getInstalUnit());
        info.setNextCheckDate(StrUtil.isBlank(emPipingDto.getNextCheckDate()) ? null : DateUtil.parseDate(emPipingDto.getNextCheckDate()));
        info.setUsedDate(StrUtil.isBlank(emPipingDto.getUsedDate()) ? null : DateUtil.parseDate(emPipingDto.getUsedDate()));
        info.setCreateTime(new Date());
        info.setIsDelete(0);
        specialEmPipingMapper.insert(info);
        return DataResult.build9100();
    }

    @Override
    public DataResult updateEmPiping(LoginInfo loginInfo, EmPipingDto emPipingDto) throws Exception {
        if(StrUtil.isBlank(emPipingDto.getId())){
            return DataResult.build9250("id不能为空");
        }
        SpecialEmPiping getSpecialEmPiping = specialEmPipingMapper.selectByPrimaryKey(Long.parseLong(emPipingDto.getId()));
        if(getSpecialEmPiping == null){
            return DataResult.build9250("查询不到数据");
        }
        SpecialEmPiping specialEmPiping = new SpecialEmPiping();
        specialEmPiping.setId(Long.parseLong(emPipingDto.getId()));
        specialEmPiping.setName(emPipingDto.getName());
        specialEmPiping.setDeviceName(emPipingDto.getDeviceName());
        specialEmPiping.setDepartment(StrUtil.isBlank(emPipingDto.getDepartment()) ? null : Integer.parseInt(emPipingDto.getDepartment()));
        specialEmPiping.setPipeCode(emPipingDto.getPipeCode());
        specialEmPiping.setCheckAccount(emPipingDto.getCheckAccount());
        specialEmPiping.setEmLocation(emPipingDto.getEmLocation());
        specialEmPiping.setEmRegion(emPipingDto.getEmRegion());
        specialEmPiping.setPipeLength(emPipingDto.getPipeLength());
        specialEmPiping.setRegisterCode(emPipingDto.getRegisterCode());
        specialEmPiping.setSpecification(emPipingDto.getSpecification());
        specialEmPiping.setInstalUnit(emPipingDto.getInstalUnit());
        specialEmPiping.setNextCheckDate(StrUtil.isBlank(emPipingDto.getNextCheckDate()) ? null : DateUtil.parseDate(emPipingDto.getNextCheckDate()));
        specialEmPiping.setUsedDate(StrUtil.isBlank(emPipingDto.getUsedDate()) ? null : DateUtil.parseDate(emPipingDto.getUsedDate()));
        specialEmPiping.setCreateTime(getSpecialEmPiping.getCreateTime());
        specialEmPiping.setIsDelete(getSpecialEmPiping.getIsDelete());
        specialEmPipingMapper.updateByPrimaryKey(specialEmPiping);
        return DataResult.build9100();
    }

    @Override
    public DataResult getEmPipingById(LoginInfo loginInfo, String id) throws Exception {
        SpecialEmPiping sep = specialEmPipingMapper.selectByPrimaryKey(Long.parseLong(id));
        Map<String,Object> map = new HashMap<>();
        map.put("id", sep.getId());
        map.put("name", sep.getName());
        map.put("deviceName", sep.getDeviceName());
        map.put("pipeCode", sep.getPipeCode());
        map.put("emLocation", sep.getEmLocation());
        map.put("pipeLength", sep.getPipeLength());
        map.put("specification", sep.getSpecification());
        map.put("emRegion", sep.getEmRegion());
        map.put("registerCode", sep.getRegisterCode());
        map.put("checkAccount", sep.getCheckAccount());
        map.put("instalUnit", sep.getInstalUnit());
        if(sep.getNextCheckDate() != null){
            map.put("nextCheckDate", DateUtil.format(sep.getNextCheckDate(), "yyyy-MM-dd"));
        }else {
            map.put("nextCheckDate", sep.getNextCheckDate());
        }
        if(sep.getNextCheckDate() != null){
            map.put("usedDate", DateUtil.format(sep.getUsedDate(), "yyyy-MM-dd"));
        }else {
            map.put("usedDate", sep.getUsedDate());
        }
        map.put("department", sep.getDepartment());
        map.put("createTime", DateUtil.format(sep.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        return DataResult.build9100(map);
    }

    @Override
    public DataResult deleteEmPiping(LoginInfo loginInfo, String id) throws Exception {
        SpecialEmPiping specialEmPiping = new SpecialEmPiping();
        specialEmPiping.setId(Long.parseLong(id));
        specialEmPiping.setIsDelete(1);
        specialEmPipingMapper.updateByPrimaryKeySelective(specialEmPiping);
        return DataResult.build9100();
    }

    @Override
    public DataResult getEmContainerList(LoginInfo loginInfo, SpecialPageAllDto specialPageAllDto) throws Exception {
        SpecialEmContainerExample example = new SpecialEmContainerExample();
        SpecialEmContainerExample.Criteria eiCriteria = example.createCriteria();
        eiCriteria.andIsDeleteEqualTo(0);
        if (StrUtil.isNotBlank(specialPageAllDto.getName())) {
            eiCriteria.andNameLike("%" + specialPageAllDto.getName() + "%");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StrUtil.isNotEmpty(specialPageAllDto.getStartTime())) {
            eiCriteria.andCreateTimeGreaterThanOrEqualTo(sdf.parse(specialPageAllDto.getStartTime() + " 00:00:00"));
        }
        if (StrUtil.isNotEmpty(specialPageAllDto.getEndTime())) {
            eiCriteria.andCreateTimeLessThanOrEqualTo(sdf.parse(specialPageAllDto.getEndTime() + " 23:59:59"));
        }
        example.setOrderByClause("create_time desc");
        Page<SpecialEmContainer> page = PageHelper.startPage(NumberUtil.parseInt(specialPageAllDto.getPageNum()), NumberUtil.parseInt(specialPageAllDto.getPageSize()));

        List<SpecialEmContainer> emPipingList = specialEmContainerMapper.selectByExample(example);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<Integer, String> typeEmState = commonService.getDictMap("TYPE_EM_STATE");
        Map<Integer, String> containerType = commonService.getDictMap("CONTAINER_TYPE");
        Map<Integer, String> containerDepartment = commonService.getDictMap("CONTAINER_DEPARTMENT");
        Map<Integer, String> containerRegisterState = commonService.getDictMap("CONTAINER_REGISTER_STATE");
        for (SpecialEmContainer sec : emPipingList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", sec.getId());
            map.put("name", sec.getName());
            map.put("emName", sec.getEmName());
            map.put("factoryCode", sec.getFactoryCode());
            map.put("life", sec.getLife());
            map.put("registerCode", sec.getRegisterCode());
            if (sec.getRegisterDate() != null) {
                map.put("registerDate", DateUtil.format(sec.getRegisterDate(), "yyyy-MM-dd"));
            } else {
                map.put("registerDate", sec.getRegisterDate());
            }
            map.put("insideDiameter", sec.getInsideDiameter());
            map.put("pressure", sec.getPressure());
            map.put("volume", sec.getVolume());
            if(sec.getNextCheckDate() != null){
                map.put("nextCheckDate", DateUtil.format(sec.getNextCheckDate(), "yyyy-MM-dd"));
            }else {
                map.put("nextCheckDate", sec.getNextCheckDate());
            }
            map.put("emRegion", sec.getEmRegion());
            map.put("checkCode", sec.getCheckCode());
            map.put("certificateCode", sec.getCertificateCode());
            map.put("sideMedium", sec.getSideMedium());
            map.put("createTime", DateUtil.format(sec.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            if(sec.getDepartment() != null){
                map.put("departmentName", containerDepartment.get(sec.getDepartment()));
            }else {
                map.put("departmentName", sec.getDepartment());
            }
            if(sec.getContainerType() != null){
                map.put("containerTypeName", containerType.get(sec.getContainerType()));
            }else {
                map.put("containerTypeName", sec.getContainerType());
            }
            if(sec.getRegisterState() != null){
                map.put("registerStateName", containerRegisterState.get(sec.getRegisterState()));
            }else {
                map.put("registerStateName", sec.getRegisterState());
            }
            map.put("emStateName", typeEmState.get(sec.getEmState()));
            list.add(map);
        }
        return DataResult.build9100(new GridData(page.getTotal(), list));
    }

    @Override
    public DataResult insertEmContainer(LoginInfo loginInfo, EmContainerDto emContainerDto) throws Exception {
        SpecialEmContainer spec = new SpecialEmContainer();
        if(StrUtil.isNotBlank(emContainerDto.getEmId())){
            EquipmentInfo equipmentInfo = equipmentInfoMapper.selectByPrimaryKey(Long.parseLong(emContainerDto.getEmId()));
            if(equipmentInfo == null){
                return DataResult.build9200("查询不到所选择的设备");
            }
            spec.setEmId(Long.parseLong(emContainerDto.getEmId()));
            spec.setEmName(equipmentInfo.getEmName());
            spec.setEmState(equipmentInfo.getEmState());
        }
        spec.setName(emContainerDto.getName());
        spec.setFactoryCode(emContainerDto.getFactoryCode());
        spec.setLife(emContainerDto.getLife() == null ? null : Integer.parseInt(emContainerDto.getLife()));
        spec.setRegisterState(emContainerDto.getRegisterState() == null ? null : Integer.parseInt(emContainerDto.getRegisterState()));
        spec.setInsideDiameter(emContainerDto.getInsideDiameter() == null ? null : Integer.parseInt(emContainerDto.getInsideDiameter()));
        spec.setPressure(emContainerDto.getPressure());
        spec.setVolume(emContainerDto.getVolume());
        spec.setDepartment(emContainerDto.getDepartment() == null ? null : Integer.parseInt(emContainerDto.getDepartment()));
        spec.setNextCheckDate(StrUtil.isBlank(emContainerDto.getNextCheckDate()) ? null : DateUtil.parseDate(emContainerDto.getNextCheckDate()));
        spec.setRegisterCode(emContainerDto.getRegisterCode());
        spec.setRegisterDate(StrUtil.isBlank(emContainerDto.getRegisterDate()) ? null : DateUtil.parseDate(emContainerDto.getRegisterDate()));
        spec.setEmRegion(emContainerDto.getEmRegion());
        spec.setCertificateCode(emContainerDto.getCertificateCode());
        spec.setCheckCode(emContainerDto.getCheckCode());
        spec.setContainerType(emContainerDto.getContainerType() == null ? null : Integer.parseInt(emContainerDto.getContainerType()));
        spec.setSideMedium(emContainerDto.getSideMedium());
        spec.setCreateTime(new Date());
        spec.setIsDelete(0);
        specialEmContainerMapper.insert(spec);
        return DataResult.build9100();
    }

    @Override
    public DataResult updateEmContainer(LoginInfo loginInfo, EmContainerDto emContainerDto) throws Exception {
        if(StrUtil.isBlank(emContainerDto.getId())){
            return DataResult.build9250("id不能为空");
        }
        SpecialEmContainer getSpecialEmContainer = specialEmContainerMapper.selectByPrimaryKey(Long.parseLong(emContainerDto.getId()));
        SpecialEmContainer spec = new SpecialEmContainer();
        spec.setId(Long.parseLong(emContainerDto.getId()));
        if (StrUtil.isNotBlank(emContainerDto.getEmId())) {
            EquipmentInfo equipmentInfo = equipmentInfoMapper.selectByPrimaryKey(Long.parseLong(emContainerDto.getEmId()));
            if (equipmentInfo == null) {
                return DataResult.build9200("查询不到所选择的设备");
            }
            spec.setEmId(Long.parseLong(emContainerDto.getEmId()));
            spec.setEmName(equipmentInfo.getEmName());
            spec.setEmState(equipmentInfo.getEmState());
        }
        spec.setName(emContainerDto.getName());
        spec.setFactoryCode(emContainerDto.getFactoryCode());
        spec.setLife(emContainerDto.getLife() == null ? null : Integer.parseInt(emContainerDto.getLife()));
        spec.setRegisterState(emContainerDto.getRegisterState() == null ? null : Integer.parseInt(emContainerDto.getRegisterState()));
        spec.setInsideDiameter(emContainerDto.getInsideDiameter() == null ? null : Integer.parseInt(emContainerDto.getInsideDiameter()));
        spec.setPressure(emContainerDto.getPressure());
        spec.setVolume(emContainerDto.getVolume());
        spec.setDepartment(emContainerDto.getDepartment() == null ? null : Integer.parseInt(emContainerDto.getDepartment()));
        spec.setNextCheckDate(StrUtil.isBlank(emContainerDto.getNextCheckDate()) ? null : DateUtil.parseDate(emContainerDto.getNextCheckDate()));
        spec.setRegisterCode(emContainerDto.getRegisterCode());
        spec.setRegisterDate(StrUtil.isBlank(emContainerDto.getRegisterDate()) ? null : DateUtil.parseDate(emContainerDto.getRegisterDate()));
        spec.setEmRegion(emContainerDto.getEmRegion());
        spec.setCertificateCode(emContainerDto.getCertificateCode());
        spec.setCheckCode(emContainerDto.getCheckCode());
        spec.setContainerType(emContainerDto.getContainerType() == null ? null : Integer.parseInt(emContainerDto.getContainerType()));
        spec.setSideMedium(emContainerDto.getSideMedium());
        spec.setCreateTime(getSpecialEmContainer.getCreateTime());
        spec.setIsDelete(getSpecialEmContainer.getIsDelete());
        specialEmContainerMapper.updateByPrimaryKey(spec);
        return DataResult.build9100();
    }

    @Override
    public DataResult getEmContainerById(LoginInfo loginInfo, String id) throws Exception {
        SpecialEmContainer sec = specialEmContainerMapper.selectByPrimaryKey(Long.parseLong(id));
        Map<String, Object> map = new HashMap<>();
        map.put("id", sec.getId());
        map.put("name", sec.getName());
        map.put("emName", sec.getEmName());
        map.put("emId", sec.getEmId());
        map.put("factoryCode", sec.getFactoryCode());
        map.put("life", sec.getLife());
        map.put("registerCode", sec.getRegisterCode());
        if (sec.getRegisterDate() != null) {
            map.put("registerDate", DateUtil.format(sec.getRegisterDate(), "yyyy-MM-dd"));
        } else {
            map.put("registerDate", sec.getRegisterDate());
        }
        map.put("insideDiameter", sec.getInsideDiameter());
        map.put("pressure", sec.getPressure());
        map.put("volume", sec.getVolume());
        if(sec.getNextCheckDate() != null){
            map.put("nextCheckDate", DateUtil.format(sec.getNextCheckDate(), "yyyy-MM-dd"));
        } else {
            map.put("nextCheckDate", sec.getNextCheckDate());
        }
        map.put("emRegion", sec.getEmRegion());
        map.put("checkCode", sec.getCheckCode());
        map.put("certificateCode", sec.getCertificateCode());
        map.put("sideMedium", sec.getSideMedium());
        map.put("createTime", DateUtil.format(sec.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        map.put("department", sec.getDepartment());
        map.put("containerType", sec.getContainerType());
        map.put("registerState", sec.getRegisterState());
        return DataResult.build9100(map);
    }

    @Override
    public DataResult deleteEmContainer(LoginInfo loginInfo, String id) throws Exception {
        SpecialEmContainer specialEmContainer = new SpecialEmContainer();
        specialEmContainer.setId(Long.parseLong(id));
        specialEmContainer.setIsDelete(1);
        specialEmContainerMapper.updateByPrimaryKeySelective(specialEmContainer);
        return DataResult.build9100();
    }

}
